package com.itcast.donkey.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.itcast.donkey.R;
import com.itcast.donkey.activity.PersonalActivity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;

/**
 * Created by lanouhn on 16/8/26.
 */
public class LoginFragment extends Fragment {

    EditText et_email;
    EditText et_pwd;

    private Button qqBtn, weiboBtn;

    private Platform qq;

    private PlatFromLis listener;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        listener = new PlatFromLis();
        sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();

        View view = inflater.inflate(R.layout.fragment_login, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        et_email = (EditText) getView().findViewById(R.id.et_login_email);
        et_pwd = (EditText) getView().findViewById(R.id.et_login_password);
        weiboBtn = (Button) getView().findViewById(R.id.ib_weibo_login);
        qqBtn = (Button) getView().findViewById(R.id.ib_qq_login);


        et_email.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        et_pwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        qqBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareSDK.initSDK(getActivity());
                qq = ShareSDK.getPlatform(QQ.NAME);
                qq.setPlatformActionListener(listener);
                qq.authorize();//授权并且获取用户信息


                authorize(qq);

            }
        });
    }

    private void authorize(Platform plat) {
        if (plat == null) {
            return;
        }
        //判断指定平台是否已经完成授权
        if (plat.isAuthValid()) {
            String userId = plat.getDb().getUserId();
            Log.i("result", "isAuthValid");
            if (userId != null) {
                Log.i("result", "userId");
                return;
            }
        }
        // true不使用SSO授权，false使用SSO授权
        plat.SSOSetting(true);
        //获取用户资料
        plat.showUser(null);
    }

    private class PlatFromLis implements PlatformActionListener {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            Iterator ite = hashMap.entrySet().iterator();
            while (ite.hasNext()) {
                Map.Entry entry = (Map.Entry) ite.next();
                Object key = entry.getKey();
                Object value = entry.getValue();
                System.out.println(key + "： " + value);
            }
            editor.putBoolean("isEnter", false);
            editor.commit();
            Intent intent = new Intent(getActivity(), PersonalActivity.class);


            startActivity(intent);
            getActivity().finish();

        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            Log.i("result", "onError");
        }

        @Override
        public void onCancel(Platform platform, int i) {
            Log.i("result", "onCancel");
        }
    }

}